package top.jle.wx.proxy.api.invoice;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.jle.wx.proxy.annotation.AccessToken;
import top.jle.wx.proxy.api.ResponseBack;
import top.jle.wx.proxy.constant.InvoiceOption;
import top.jle.wx.proxy.domain.invoice.CardInfo;
import top.jle.wx.proxy.domain.invoice.Invoice;
import top.jle.wx.proxy.facade.InvoiceFacade;

@RestController
@RequestMapping("/invoice")
@Tag(name="微信发票API",description = "只处理插入卡包 不负责开票，开票行为由开票平台负责")
@AccessToken
public class InvoiceAPI {

    @Autowired
    private InvoiceFacade invoiceFacade;

    @Operation(summary = "创建电子发票卡券，返回卡券ID")
    @PostMapping("/create")
    public ResponseBack<String> createCard(@Parameter(description="卡券信息") @RequestBody CardInfo cardInfo){
        return ResponseBack.success(invoiceFacade.createCard(cardInfo));
    }
    @Operation(summary = "上传电子发票PDF，返回媒体ID，插卡使用")
    @PostMapping("/upload_pdf")
    public ResponseBack<String> uploadPDF(@RequestPart("pdf") MultipartFile pdf){
        return ResponseBack.success(invoiceFacade.uploadPDF(pdf));
    }

    @Operation(summary = "获取电子发票PDF的URL")
    @GetMapping("/get_pdf/{mediaId}")
    public ResponseBack<String> getPDFUrl(@Parameter(description="PDF媒体ID") @PathVariable String mediaId){
        return ResponseBack.success(invoiceFacade.getPDF(mediaId));
    }

    @Operation(summary = "将发票插入粉丝卡包")
    @PostMapping("/insert")
    public ResponseBack<InvoiceOption.InsertInvoiceResponse> insert(@Parameter(description="订单编号") @RequestParam String orderNo,
                                                                    @Parameter(description="卡券ID") @RequestParam String cardId,
                                                                    @Parameter(description="插卡信息") @RequestBody Invoice invoice){
        return ResponseBack.success(invoiceFacade.insertInvoice(orderNo,cardId,invoice));
    }
}
